package pe.edu.upc.dsd.epica.util;

import twitter4j.AccountSettings;
import twitter4j.GeoLocation;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import android.content.SharedPreferences;

public class TwitterUtils implements ProviderUtil {
	
	public String getNickName(SharedPreferences prefs) {

		String[] tokens = new SharedPreferencesCredentialStore(prefs).read();
		AccessToken a = new AccessToken(tokens[0], tokens[1]);
		Twitter twitter = new TwitterFactory().getInstance();
		twitter.setOAuthConsumer(ConstantsTwitter.CONSUMER_KEY,ConstantsTwitter.CONSUMER_SECRET);
		twitter.setOAuthAccessToken(a);

		try {
			return twitter.getScreenName();
		} catch (TwitterException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public String getNickName(String consumerkey, String consumersecret) {

		AccessToken a = new AccessToken(consumerkey, consumersecret);
		Twitter twitter = new TwitterFactory().getInstance();
		twitter.setOAuthConsumer

(ConstantsTwitter.CONSUMER_KEY,ConstantsTwitter.CONSUMER_SECRET);
		twitter.setOAuthAccessToken(a);

		try {
			return twitter.getScreenName();
		} catch (TwitterException e) {
			e.printStackTrace();
			return null;
		}
	}

	public boolean isAuthenticated(SharedPreferences prefs) {

		String[] tokens = new SharedPreferencesCredentialStore(prefs).read();
		AccessToken a = new AccessToken(tokens[0], tokens[1]);
		Twitter twitter = new TwitterFactory().getInstance();
		twitter.setOAuthConsumer(ConstantsTwitter.CONSUMER_KEY,ConstantsTwitter.CONSUMER_SECRET);
		twitter.setOAuthAccessToken(a);

		try {
			AccountSettings settings = twitter.getAccountSettings();
			return settings != null;
		} catch (TwitterException e) {
			e.printStackTrace();
			return false;
		}
	}

	public void sendMessage(SharedPreferences prefs, String msg,double latitude, double longitude) throws Exception {
		String[] tokens = new SharedPreferencesCredentialStore(prefs).read();
		AccessToken a = new AccessToken(tokens[0], tokens[1]);
		Twitter twitter = new TwitterFactory().getInstance();
		twitter.setOAuthConsumer(ConstantsTwitter.CONSUMER_KEY,ConstantsTwitter.CONSUMER_SECRET);
		twitter.setOAuthAccessToken(a);

		GeoLocation location = new GeoLocation(latitude, longitude);
		twitter.updateStatus(msg, location);
	}
	
	public void sendMessage(String consumerkey, String consumersecret, String msg,double latitude, double longitude) throws Exception {
		AccessToken a = new AccessToken(consumerkey, consumersecret);
		Twitter twitter = new TwitterFactory().getInstance();
		twitter.setOAuthConsumer(ConstantsTwitter.CONSUMER_KEY,ConstantsTwitter.CONSUMER_SECRET);
		twitter.setOAuthAccessToken(a);

		GeoLocation location = new GeoLocation(latitude, longitude);
		twitter.updateStatus(msg, location);
	}
	
	public void sendMessage(String consumerkey, String consumersecret, String msg) throws Exception {
		AccessToken a = new AccessToken(consumerkey, consumersecret);
		Twitter twitter = new TwitterFactory().getInstance();
		twitter.setOAuthConsumer(ConstantsTwitter.CONSUMER_KEY,ConstantsTwitter.CONSUMER_SECRET);
		twitter.setOAuthAccessToken(a);

		
		twitter.updateStatus(msg);
	}
}
